# coding=utf-8
"""
    @project: 15python_spider
    @Author：frank
    @file： jd_selenium.py
    @date：2024/3/18 20:15
"""
from selenium import webdriver
import time

from selenium.webdriver.common.by import By


class JdSpider(object):
    def __init__(self):
        self.browser = webdriver.Chrome()
        self.url = 'https://www.jd.com/'

    # 获取商品页面
    def get_page(self):
        # 打开京东
        self.browser.get(self.url)
        # 找到两个节点
        self.browser.find_element(by=By.XPATH, value="//*[@id='key']").send_keys('爬虫书籍')
        self.browser.find_element(by=By.XPATH, value="//*[@id='search']/div/div[2]/button").click()
        # 留出时间给页面加载
        time.sleep(20)

    # 解析页面
    def parse_page(self):
        # 把进度条拉到最下面
        self.browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')
        time.sleep(3)
        # 匹配所有商品节点对象列表
        li_list = self.browser.find_elements(by=By.XPATH, value="//*[@id='J_goodsList']/ul/li")
        for li in li_list:
            print(li.text)
            print('*' * 50)

    def main(self):
        self.get_page()
        while True:
            self.parse_page()
            if self.browser.page_source.find("pn-next disabled") == -1:
                self.browser.find_element(by=By.CLASS_NAME, value="pn-next").click()
                time.sleep(5)
            else:
                break
if __name__ == '__main__':
    jd_spider = JdSpider()
    jd_spider.main()
